
//
// 101120 - AH - Created.
// 101124a- AH - (Garage) showDialog(String).
// 101124b- AH - (Garage) above new method also accepts resource:
//               showDialog(String, AttributeObject).
// 101127 - AH - Now constructors accepts SoekratEngine and HistoricalEvent
//               objects are generated.
// 101217 - AH - Resources are now of type Resource, not AttributeObject 
//               directly.
// 101228 - AH - Minor java doc fix, bug discovered by java doc compiler.
// 110605 - AH - (Garage) Changes related to that resource name is now a 
//               SoekrateName.
// 110807 - AH - Deprecated. TODO Delete.
//

package org.soekrat.gui.swing.resource;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.SoekratName;
import org.soekrat.engine.mine.Resource;
import org.soekrat.engine.mine.Tags;

/**
 * <p>
 *   Dialog for creating resources.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@Deprecated
@SuppressWarnings("serial")
public final class CreateResourceDialog extends JDialog 
{
  private final CreateResourcePanel m_crp=new CreateResourcePanel();
  
  private Resource m_created=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param engine {@link Soekrat2Engine} interfaced. 
   */
  public CreateResourceDialog(Soekrat2Engine engine){ buildGui(engine); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param engine {@link Soekrat2Engine} interfaced. 
   * @param p      Dialog's parent.
   */
  public CreateResourceDialog(Soekrat2Engine engine, Frame p)
  { 
    super(p); 
    buildGui(engine); 
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p      Dialog's parent.
   * @param engine {@link Soekrat2Engine} interfaced.
   */
  public CreateResourceDialog(Dialog p, Soekrat2Engine engine)
  { 
    super(p); 
    buildGui(engine); 
  }

  // Invoked from constructors to build gui.
  private void buildGui(final Soekrat2Engine engine)
  {
    if (engine==null)
    {
      throw new NullPointerException("engine");
    }
    
    setTitle("Create Resource");
    setModal(true);
    
    Container c=getContentPane();
    c.setLayout(new BorderLayout());
    
    c.add(m_crp, BorderLayout.CENTER);
    
    JPanel buttons=new JPanel(new FlowLayout(FlowLayout.CENTER));
    buttons.add
    (
      new JButton
      (
        new AbstractAction("OK") 
        {
          public void actionPerformed(ActionEvent e)
          {
            m_created=null;
            String err=m_crp.create();
            if (err==null)
            {
              m_created=m_crp.getCreated();
              dispose();
            }
            else
            {
              JOptionPane.showMessageDialog(CreateResourceDialog.this, err, 
                "Invalis Input", JOptionPane.ERROR_MESSAGE);
            }
          }
        }
      )
    );
    buttons.add
    (
      new JButton
      (
        new AbstractAction("Cancel") 
        {
          public void actionPerformed(ActionEvent e)
          { 
            m_created=null; 
            dispose();
          }
        }
      )
    );
    c.add(buttons, BorderLayout.SOUTH);
    
    pack();
  }
  
  /**
   * <p>
   *   Shows dialog.
   * </p>
   * @return Created resource or {@code null} if none was created.
   */
  public Resource showDialog(){ return showDialog(null, null); }
  
  /**
   * <p>
   *   Shows dialog.
   * </p>
   * @param title    Dialog title, may be {@code null} then uses default
   *                 "Create Resource".
   * @param resource Resource to copy name and tags from, may be {@code null}.              
   * @return Created resource or {@code null} if none was created.
   */
  public Resource showDialog(String title, Resource resource)
  { 
    title=(title==null) ? "Create Resource" : title;
    
    setTitle(title);
    
    m_crp.clear();
    
    if (resource!=null)
    {
      SoekratName sname=resource.getSoekratName();
      
      String name=sname.toString();
      
      Tags tags=(Tags)resource.getAttribute("tags");
      m_crp.setName(name);
      m_crp.setTags(tags);
    }
     
    setVisible(true); 
    return m_created; 
  }
  
}
